//
//  FirstViewController.m
//  NativeAppWithFlutter
//
//  Created by 门超 on 2021/3/24.
//

#import "FirstViewController.h"
#import <Flutter/Flutter.h>

#define Screen_width [UIScreen mainScreen].bounds.size.width
#define Screen_height [UIScreen mainScreen].bounds.size.height



@interface FirstViewController ()<FlutterStreamHandler>
@property (nonatomic, copy) FlutterEventSink eventSink;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    self.title = @"原生FirstViewController页面";
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(40, 150, Screen_width-80, 36)];
//    btn.layer.cornerRadius = 15;
//    btn.layer.masksToBounds = YES;
    btn.backgroundColor = UIColor.redColor;
    [btn setTitle:@"调用flutter页面-->iOS主动和flutter通信" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnClicked{
    FlutterDartProject * dartProject = [[FlutterDartProject alloc] init];
    FlutterViewController *flutterVC =   [[FlutterViewController alloc]initWithProject:dartProject initialRoute:@"home" nibName:nil bundle:nil];
    NSString *eventChannelName = @"navitve-post-flutter";
    NSString *methodChannelName = @"navitve-get-flutter";
    
    /*---------FlutterEventChannel是IOS主动与flutter交互-----------*/
    FlutterEventChannel *eventChannel = [FlutterEventChannel eventChannelWithName:eventChannelName binaryMessenger:flutterVC];
    
    // 代理FlutterStreamHandler
    [eventChannel setStreamHandler:self];
    /*---------FlutterEventChannel是IOS主动与flutter交互-----------*/

    
    /*---------FlutterMethodChannel是flutter主动与IOS交互-----------*/
    FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:methodChannelName binaryMessenger:flutterVC];
    [methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        
        
        NSLog(@"---->call.method= %@ , call.arguments= %@ ", call.method, call.arguments);
        
        if ([call.method isEqualToString:@"changeNavStatus"]) {
            NSString *arguments = call.arguments;
            if ([arguments containsString:@"show"]) {
                self.navigationController.navigationBarHidden = NO;
            }else{
                self.navigationController.navigationBarHidden = YES;
            }
        }
        if ([call.method isEqualToString:@"backToViewController"]) {
           
            NSArray *viewcontrollers=self.navigationController.viewControllers;
            if(viewcontrollers.count>1) {
                if([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }else{
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }
        }
    }];
  
}

#pragma mark - <FlutterStreamHandler>
//这个onListen是Flutter端开始监听这个channel时的回调，第二个参数 EventSink是用来传数据的载体。
-(FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events{
    NSLog(@"onListenWithArguments..");
    self.eventSink = events;
    // arguments flutter监听时,给native传送的参数
    if (self.eventSink) {
        self.eventSink(@"我是原生push到fluttet页面,传给flutter的显示的值");
    }
    return nil;
}

// flutter不再接收
-(FlutterError *)onCancelWithArguments:(id)arguments{
    
    //
    self.eventSink = nil;
    return nil;
}

@end
